package com.zy.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: shopping153_cloud
 * @description: 上传文件类型的校验器
 * @author: zy
 * @create: 2025-08-19 15:07
 */
public class FileTypeValidator {

    /**
     * 允许上传的文件的MIME类型( http协议中定义的文件类型 )
     */
    private static final Set<String> ALLOWED_MIME_TYPES = new HashSet<>(Arrays.asList(
            "image/jpeg",
            "image/png",
            "image/gif",
            "application/pdf",
            "application/msword", // .doc
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
            "application/vnd.ms-excel", // .xls
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // .xlsx
    ));

    /**
     * 允许上传的文件的后缀名
     */
    private static final Set<String> ALLOWED_EXTENSIONS = new HashSet<>(Arrays.asList(
            "jpg", "jpeg", "png", "gif",
            "pdf",
            "doc", "docx",
            "xls", "xlsx"
    ));

    /**
     * 校验文件类型是否合法
     * @param contentType ： MIME类型
     * @param fileName   :文件名（主要判断文件的后缀名）
     * @return
     */
    public static boolean isValidType(   String contentType, String fileName ){
        if (contentType == null || fileName == null || contentType.isEmpty() || fileName.isEmpty()) {
            throw new IllegalArgumentException("文件类型或文件名不能为空");
        }
        // 校验MIME类型
        if (!ALLOWED_MIME_TYPES.contains(contentType)) {
            return false;
        }
        // 校验文件扩展名
        String fileExtension = getFileExtension(fileName);
        if (fileExtension == null || !ALLOWED_EXTENSIONS.contains(fileExtension.toLowerCase())) {
            return false;
        }
        return true;
    }



    /**
     * 获取文件扩展名
     * @param filename 文件名
     * @return 文件扩展名
     */
    private static String getFileExtension(String filename) {
        int dotIndex = filename.lastIndexOf('.');
        if (dotIndex > 0 && dotIndex < filename.length() - 1) {
            return filename.substring(dotIndex + 1);
        }
        return null;
    }
}
